﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProgNetComponents.Data;
using System.Data;

namespace ProgNetDxComponents.ExtenderProviders.Options
{
    public class DBOptionGrabber : OptionGrabber
    {
        private DataTable _Options;
        protected DataTable Options
        {
            get
            {
                if (_Options == null)
                {
                    _Options = CreateOptionsTable(TableName);
                    _GetAllOptions();
                }
                return _Options;
            }
        }

        public string NameColumn
        {
            get;
            set;
        }
        public string ValueColumn
        {
            get;
            set;
        }
        public string TableName
        {
            get;
            set;
        }

        public DBOptionGrabber()
        {
        }

        public DataManager DataManager
        {
            get;
            set;
        }

        protected virtual DataTable CreateOptionsTable(string name)
        {
            DataTable d = new DataTable(name);
            d.Columns.Add(NameColumn);
            d.Columns.Add(ValueColumn);
            return d;
        }

        protected virtual string GetSelectString()
        {
            return "SELECT " + NameColumn + ", " + ValueColumn + " FROM " + TableName;
        }

        public override bool HasOption(string name)
        {
            return Options.RowExists(NameColumn + " = " + name.EscapeSql());
        }

        protected override bool _SetOptionValue(string name, object value)
        {
            DataRow r = Options.SelectFirstRow(NameColumn + " = " + name.EscapeSql());
            if (r != null)
            {
                if (r[ValueColumn] != (value != null ? value : DBNull.Value))
                {
                    r[ValueColumn] = value != null ? value : DBNull.Value;
                    return true;
                }
            }
            else
            {
                r = Options.NewRow();
                r[NameColumn] = name;
                r[ValueColumn] = value != null ? value : DBNull.Value;
                Options.Rows.Add(r);
                return true;
            }
            return false;
        }

        public override object GetOptionValue(string name)
        {
            DataRow r = Options.SelectFirstRow(NameColumn + " = " + name.EscapeSql());
            if (r != null)
            {
                return r[ValueColumn];
            }
            return null;
        }

        protected override bool _SetOptionValueDirect(string name, object value)
        {
            bool result = false;
            SetOptionValue(name, value);
            DataRow r = Options.SelectFirstRow(NameColumn + " = " + name.EscapeSql());
            if (r != null)
            {
                DataTable dt = Options.Clone();
                dt.ImportRow(r);


                result = DataManager.UpdateTable(dt);
                if (result)
                    r.AcceptChanges();
            }
            return result;
        }

        protected override void _GetAllOptions()
        {
            Options.Clear();
            DataManager.FillTable(Options, GetSelectString());
            Options.AcceptChanges();
        }

        protected override bool SaveOptions()
        {
            bool result = DataManager.UpdateTable(Options);
            if (result)
                Options.AcceptChanges();
            return result;
        }
    }
}
